﻿using InnoWeb.CEND.BL.Service;
using InnoWeb.CEND.Core.Model.General;
using InnoWeb.CEND.Data;
using InnoWeb.CEND.Data.Infrastructure;
using InnoWeb.CEND.Web.Framework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace InnoWeb.CEND.UI.MVC.Areas.Admin.Controllers
{
    public class SolutionPageController : Controller
    {
        private static WebSiteContext _context;
        private static IUnitOfWork _unitOfWork;

        private static GenericRepository<SolutionPageContent> _solutionPageRepo;
        private static SolutionPageContentService _solutionService;

        public SolutionPageController()
        {
            _context = new WebSiteContext(ConfigurationManager.ConnectionStrings["CendDBConnection"].ConnectionString);
            _unitOfWork = new UnitOfWork(_context);

            _solutionPageRepo = new GenericRepository<SolutionPageContent>(_context);
            _solutionService = new SolutionPageContentService(_unitOfWork, _solutionPageRepo);
        }


        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Details(int id)
        {
            return View();
        }

        #region Solution Page Creates

        public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion

        #region Solution Page Edits
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditConsulting(int id)
        {
            var item = _solutionService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditConsulting(int id, SolutionPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                SolutionPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.ConsultingImage =  fileName + ".jpg";
                }
                else
                {
                    item.ConsultingImage = _solutionService.GetElementById(model.Id).ConsultingImage;
                }

                _solutionService.UpdateElement(item);
                _solutionService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditManConsulting(int id)
        {
            var item = _solutionService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditManConsulting(int id, SolutionPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                SolutionPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.ManagementConsultingImage =  fileName + ".jpg";
                }
                else
                {
                    item.ManagementConsultingImage = _solutionService.GetElementById(model.Id).ManagementConsultingImage;
                }

                _solutionService.UpdateElement(item);
                _solutionService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditStrMarketing(int id)
        {
            var item = _solutionService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditStrMarketing(int id, SolutionPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                SolutionPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.StrMarketingImage =  fileName + ".jpg";
                }
                else
                {
                    item.StrMarketingImage = _solutionService.GetElementById(model.Id).StrMarketingImage;
                }

                _solutionService.UpdateElement(item);
                _solutionService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditSales(int id)
        {
            var item = _solutionService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditSales(int id, SolutionPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                SolutionPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                     fileName = Guid.NewGuid().ToString().Replace("-", "");
                     serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                     thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                     fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.SalesImage =  fileName + ".jpg";
                }
                else
                {
                    item.SalesImage = _solutionService.GetElementById(model.Id).SalesImage;
                }

                _solutionService.UpdateElement(item);
                _solutionService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditSupport(int id)
        {
            var item = _solutionService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditSupport(int id, SolutionPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                SolutionPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.SupportServiceImage =  fileName + ".jpg";
                }
                else
                {
                    item.SupportServiceImage = _solutionService.GetElementById(model.Id).SupportServiceImage;
                }

                _solutionService.UpdateElement(item);
                _solutionService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditOutsource(int id)
        {
            var item = _solutionService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditOutsource(int id, SolutionPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                SolutionPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.OutSourcingImage =  fileName + ".jpg";
                }
                else
                {
                    item.OutSourcingImage = _solutionService.GetElementById(model.Id).OutSourcingImage;
                }

                _solutionService.UpdateElement(item);
                _solutionService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditTraining(int id)
        {
            var item = _solutionService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditTraining(int id, SolutionPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                SolutionPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.TrainingEducationImage =  fileName + ".jpg";
                }
                else
                {
                    item.TrainingEducationImage = _solutionService.GetElementById(model.Id).TrainingEducationImage;
                }

                _solutionService.UpdateElement(item);
                _solutionService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
        public ActionResult EditSolutions(int id)
        {
            var item = _solutionService.GetAllElements().First();
            return View(item);
        }

        [HttpPost]
        public ActionResult EditSolutions(int id, SolutionPageContent model)
        {
            try
            {
                var img = Request.Files["fileUpload"];
                string fileName, serverPath, imagesPath, thumbPath, fullPath;
                SolutionPageContent item = model;
                item.ModifiedDate = DateTime.Today;

                if (img.ContentLength > 0)
                {
                    fileName = Guid.NewGuid().ToString().Replace("-", "");
                    serverPath = Server.MapPath("~/Content/Images");
                    //string imagesPath = serverPath + "Content\\Images\\";
                    thumbPath = Server.MapPath("~/Content/Images/Thumb/");
                    fullPath = Server.MapPath("~/Content/Images/Full/");

                    ImageUploader.ImageSave(thumbPath, fullPath, img, fileName);

                    item.SolutionsImage = fileName + ".jpg";
                }
                else
                {
                    item.SolutionsImage = _solutionService.GetElementById(model.Id).SolutionsImage;
                }

                _solutionService.UpdateElement(item);
                _solutionService.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion

        #region Solution Page Deletes
        public ActionResult Delete(int id)
        {
            return View();
        }


        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        #endregion
    }
}
